/**
 * Copyleft (C) 2023 maminjie <canpool@163.com>
 * SPDX-License-Identifier: BSD-3-Clause
 **/
#include "materialtheme.h"
#include "materialtheme_p.h"
#include <QDebug>
#include <QString>
#include <QStringBuilder>

QX_MATERIAL_BEGIN_NAMESPACE

/*!material
 *  \class MaterialThemePrivate
 *  \internal
 */

MaterialThemePrivate::MaterialThemePrivate(MaterialTheme *q)
    : q_ptr(q)
{
}

MaterialThemePrivate::~MaterialThemePrivate()
{
}

/*!
 *  \class MaterialTheme
 */

MaterialTheme::MaterialTheme(QObject *parent) : QObject(parent), d_ptr(new MaterialThemePrivate(this))
{
    setColor(MK_primary1,        QxMaterial::cyan500);
    setColor(MK_primary2,        QxMaterial::cyan700);
    setColor(MK_primary3,        QxMaterial::lightBlack);
    setColor(MK_accent1,         QxMaterial::pinkA200);
    setColor(MK_accent2,         QxMaterial::grey100);
    setColor(MK_accent3,         QxMaterial::grey500);
    setColor(MK_text,            QxMaterial::darkBlack);
    setColor(MK_textAlt,         QxMaterial::white);
    setColor(MK_canvas,          QxMaterial::white);
    setColor(MK_border,          QxMaterial::grey300);
    setColor(MK_disabled1,       QxMaterial::minBlack);
    setColor(MK_disabled2,       QxMaterial::faintBlack);
    setColor(MK_disabled3,       QxMaterial::grey300);
}

MaterialTheme::~MaterialTheme()
{
}

QList<QString> MaterialTheme::colorKeys() const
{
    Q_D(const MaterialTheme);
    return d->colors.keys();
}

QColor MaterialTheme::getColor(const QString &key) const
{
    Q_D(const MaterialTheme);

    if (!d->colors.contains(key)) {
        qWarning() << "A theme color matching the key '" << key << "' could not be found.";
        return QColor();
    }
    return d->colors.value(key);
}

void MaterialTheme::setColor(const QString &key, const QColor &color)
{
    Q_D(MaterialTheme);
    d->colors.insert(key, color);
}

void MaterialTheme::setColor(const QString &key, QxMaterial::Color color)
{
    Q_D(MaterialTheme);
    d->colors.insert(key, this->color(color));
}

QIcon MaterialTheme::icon(QString category, QString icon)
{
    return QIcon(":/res/icons/" % category % "/svg/production/ic_" % icon % "_24px.svg");
}

const QColor &MaterialTheme::color(QxMaterial::Color color)
{
    static const QColor palette[] = {
        QColor("#ffebee"), // red
        QColor("#ffcdd2"),
        QColor("#ef9a9a"),
        QColor("#e57373"),
        QColor("#ef5350"),
        QColor("#f44336"),
        QColor("#e53935"),
        QColor("#d32f2f"),
        QColor("#c62828"),
        QColor("#b71c1c"),
        QColor("#ff8a80"),
        QColor("#ff5252"),
        QColor("#ff1744"),
        QColor("#d50000"),
        QColor("#fce4ec"), // pink
        QColor("#f8bbd0"),
        QColor("#f48fb1"),
        QColor("#f06292"),
        QColor("#ec407a"),
        QColor("#e91e63"),
        QColor("#d81b60"),
        QColor("#c2185b"),
        QColor("#ad1457"),
        QColor("#880e4f"),
        QColor("#ff80ab"),
        QColor("#ff4081"),
        QColor("#f50057"),
        QColor("#c51162"),
        QColor("#f3e5f5"), // purple
        QColor("#e1bee7"),
        QColor("#ce93d8"),
        QColor("#ba68c8"),
        QColor("#ab47bc"),
        QColor("#9c27b0"),
        QColor("#8e24aa"),
        QColor("#7b1fa2"),
        QColor("#6a1b9a"),
        QColor("#4a148c"),
        QColor("#ea80fc"),
        QColor("#e040fb"),
        QColor("#d500f9"),
        QColor("#aa00ff"),
        QColor("#ede7f6"), // deepPurple
        QColor("#d1c4e9"),
        QColor("#b39ddb"),
        QColor("#9575cd"),
        QColor("#7e57c2"),
        QColor("#673ab7"),
        QColor("#5e35b1"),
        QColor("#512da8"),
        QColor("#4527a0"),
        QColor("#311b92"),
        QColor("#b388ff"),
        QColor("#7c4dff"),
        QColor("#651fff"),
        QColor("#6200ea"),
        QColor("#e8eaf6"), // indigo
        QColor("#c5cae9"),
        QColor("#9fa8da"),
        QColor("#7986cb"),
        QColor("#5c6bc0"),
        QColor("#3f51b5"),
        QColor("#3949ab"),
        QColor("#303f9f"),
        QColor("#283593"),
        QColor("#1a237e"),
        QColor("#8c9eff"),
        QColor("#536dfe"),
        QColor("#3d5afe"),
        QColor("#304ffe"),
        QColor("#e3f2fd"), // blue
        QColor("#bbdefb"),
        QColor("#90caf9"),
        QColor("#64b5f6"),
        QColor("#42a5f5"),
        QColor("#2196f3"),
        QColor("#1e88e5"),
        QColor("#1976d2"),
        QColor("#1565c0"),
        QColor("#0d47a1"),
        QColor("#82b1ff"),
        QColor("#448aff"),
        QColor("#2979ff"),
        QColor("#2962ff"),
        QColor("#e1f5fe"), // lightBlue
        QColor("#b3e5fc"),
        QColor("#81d4fa"),
        QColor("#4fc3f7"),
        QColor("#29b6f6"),
        QColor("#03a9f4"),
        QColor("#039be5"),
        QColor("#0288d1"),
        QColor("#0277bd"),
        QColor("#01579b"),
        QColor("#80d8ff"),
        QColor("#40c4ff"),
        QColor("#00b0ff"),
        QColor("#0091ea"),
        QColor("#e0f7fa"), // cyan
        QColor("#b2ebf2"),
        QColor("#80deea"),
        QColor("#4dd0e1"),
        QColor("#26c6da"),
        QColor("#00bcd4"),
        QColor("#00acc1"),
        QColor("#0097a7"),
        QColor("#00838f"),
        QColor("#006064"),
        QColor("#84ffff"),
        QColor("#18ffff"),
        QColor("#00e5ff"),
        QColor("#00b8d4"),
        QColor("#e0f2f1"), // teal
        QColor("#b2dfdb"),
        QColor("#80cbc4"),
        QColor("#4db6ac"),
        QColor("#26a69a"),
        QColor("#009688"),
        QColor("#00897b"),
        QColor("#00796b"),
        QColor("#00695c"),
        QColor("#004d40"),
        QColor("#a7ffeb"),
        QColor("#64ffda"),
        QColor("#1de9b6"),
        QColor("#00bfa5"),
        QColor("#e8f5e9"), // green
        QColor("#c8e6c9"),
        QColor("#a5d6a7"),
        QColor("#81c784"),
        QColor("#66bb6a"),
        QColor("#4caf50"),
        QColor("#43a047"),
        QColor("#388e3c"),
        QColor("#2e7d32"),
        QColor("#1b5e20"),
        QColor("#b9f6ca"),
        QColor("#69f0ae"),
        QColor("#00e676"),
        QColor("#00c853"),
        QColor("#f1f8e9"), // lightGreen
        QColor("#dcedc8"),
        QColor("#c5e1a5"),
        QColor("#aed581"),
        QColor("#9ccc65"),
        QColor("#8bc34a"),
        QColor("#7cb342"),
        QColor("#689f38"),
        QColor("#558b2f"),
        QColor("#33691e"),
        QColor("#ccff90"),
        QColor("#b2ff59"),
        QColor("#76ff03"),
        QColor("#64dd17"),
        QColor("#f9fbe7"), // lime
        QColor("#f0f4c3"),
        QColor("#e6ee9c"),
        QColor("#dce775"),
        QColor("#d4e157"),
        QColor("#cddc39"),
        QColor("#c0ca33"),
        QColor("#afb42b"),
        QColor("#9e9d24"),
        QColor("#827717"),
        QColor("#f4ff81"),
        QColor("#eeff41"),
        QColor("#c6ff00"),
        QColor("#aeea00"),
        QColor("#fffde7"), // yellow
        QColor("#fff9c4"),
        QColor("#fff59d"),
        QColor("#fff176"),
        QColor("#ffee58"),
        QColor("#ffeb3b"),
        QColor("#fdd835"),
        QColor("#fbc02d"),
        QColor("#f9a825"),
        QColor("#f57f17"),
        QColor("#ffff8d"),
        QColor("#ffff00"),
        QColor("#ffea00"),
        QColor("#ffd600"),
        QColor("#fff8e1"), // amber
        QColor("#ffecb3"),
        QColor("#ffe082"),
        QColor("#ffd54f"),
        QColor("#ffca28"),
        QColor("#ffc107"),
        QColor("#ffb300"),
        QColor("#ffa000"),
        QColor("#ff8f00"),
        QColor("#ff6f00"),
        QColor("#ffe57f"),
        QColor("#ffd740"),
        QColor("#ffc400"),
        QColor("#ffab00"),
        QColor("#fff3e0"), // orange
        QColor("#ffe0b2"),
        QColor("#ffcc80"),
        QColor("#ffb74d"),
        QColor("#ffa726"),
        QColor("#ff9800"),
        QColor("#fb8c00"),
        QColor("#f57c00"),
        QColor("#ef6c00"),
        QColor("#e65100"),
        QColor("#ffd180"),
        QColor("#ffab40"),
        QColor("#ff9100"),
        QColor("#ff6d00"),
        QColor("#fbe9e7"), // deepOrange
        QColor("#ffccbc"),
        QColor("#ffab91"),
        QColor("#ff8a65"),
        QColor("#ff7043"),
        QColor("#ff5722"),
        QColor("#f4511e"),
        QColor("#e64a19"),
        QColor("#d84315"),
        QColor("#bf360c"),
        QColor("#ff9e80"),
        QColor("#ff6e40"),
        QColor("#ff3d00"),
        QColor("#dd2c00"),
        QColor("#efebe9"), // brown
        QColor("#d7ccc8"),
        QColor("#bcaaa4"),
        QColor("#a1887f"),
        QColor("#8d6e63"),
        QColor("#795548"),
        QColor("#6d4c41"),
        QColor("#5d4037"),
        QColor("#4e342e"),
        QColor("#3e2723"),
        QColor("#eceff1"), // blueGrey
        QColor("#cfd8dc"),
        QColor("#b0bec5"),
        QColor("#90a4ae"),
        QColor("#78909c"),
        QColor("#607d8b"),
        QColor("#546e7a"),
        QColor("#455a64"),
        QColor("#37474f"),
        QColor("#263238"),
        QColor("#fafafa"), // grey
        QColor("#f5f5f5"),
        QColor("#eeeeee"),
        QColor("#e0e0e0"),
        QColor("#bdbdbd"),
        QColor("#9e9e9e"),
        QColor("#757575"),
        QColor("#616161"),
        QColor("#424242"),
        QColor("#212121"),
        QColor("#000000"), // other
        QColor("#ffffff"),
        rgba(0, 0, 0, 0),
        rgba(0, 0, 0, 0.87),
        rgba(0, 0, 0, 0.54),
        rgba(0, 0, 0, 0.26),
        rgba(0, 0, 0, 0.12),
        rgba(255, 255, 255, 0.87),
        rgba(255, 255, 255, 0.54),
    };
    return palette[color];
}

QColor MaterialTheme::rgba(int r, int g, int b, qreal a)
{
    QColor color(r, g, b);
    color.setAlphaF(a);
    return color;
}

QX_MATERIAL_END_NAMESPACE
